package com.boss.example.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import com.boss.example.entity.dto.UserDTO;
import com.boss.example.mapper.UserMapper;
import com.boss.example.service.UserService;
import com.boss.example.exception.UsersException;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper usersMapper;

    /**
     * 用户登录
     */
    @Override
    public UserDTO userLogin(String username, String userpwd) {
        UserDTO userDTO = usersMapper.selectUserByName(username);
        if(userDTO == null){
            throw new UsersException("您的用户名不存在");
        }else if(!userDTO.getUserPwd().equals(userpwd)){
            throw new UsersException("您的密码错误");
        }
        return userDTO;
    }

    /**
     * 注销用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userDelete(String username) {
        return usersMapper.deleteUser(username);
    }

    /**
     * 添加用户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userInsert(UserDTO userDTO) {
        return  usersMapper.insertUser(userDTO);
    }

    /**
     * 修改用户密码
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userUpdatePwd(UserDTO userDTO) {
        return usersMapper.updateUserPwd(userDTO);
    }

    /**
     * 修改用户权限
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int userUpdateRole(UserDTO userDTO) {
        return  usersMapper.updateUserRole(userDTO);
    }

    /**
     * 查询所有用户
     */
    @Override
    public List<UserDTO> getAllUser() {
        return usersMapper.getAllUser();
    }

    /**
     * 根据ID查询用户
     */
    @Override
    public UserDTO getUserById(Integer userId) {
        return usersMapper.getUserById(userId);
    }

    /**
     * 根据ID判断用户是否为管理员
     */
    @Override
    public UserDTO getUserIsAdmin(Integer userId) {
        return usersMapper.getUserIsAdmin(userId);
    }

}
